package com.example.commen.base.dialoglib.dialog;


import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Window;
import android.view.WindowManager;


import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.commen.databinding.DialogSigonpickerBinding;
import com.example.commen.interf.ICell;
import com.example.commen.utile.CommonUtils;
import com.hjq.toast.Toaster;

import java.util.ArrayList;


/**
 * 底部弹出单项选择器
 */
public class BottomSinglePickerDialog extends Dialog {


    private ICell.ICell2<Integer,String> onClickListener;
    private boolean outIsHind = true;
    private ArrayList<String> message;
    private String title = "";
    private int CurrentItem = 0;
    private Context context;
    private DialogSigonpickerBinding bind;

    private BottomSinglePickerDialog(Context context, int theme) {
        super(context, theme);
        this.context = context;
    }


    public BottomSinglePickerDialog(Context context, String title, int CurrentItem, ArrayList<String> message, boolean outIsHind, ICell.ICell2<Integer,String> onClickListener) {
        this(context, com.qmuiteam.qmui.R.style.QMUI_BottomSheet);
        this.message = message;
        this.title = title;
        this.CurrentItem = CurrentItem;
        this.outIsHind = outIsHind;
        this.context = context;
        this.onClickListener = onClickListener;
    }

    public BottomSinglePickerDialog(Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bind = DialogSigonpickerBinding.inflate(LayoutInflater.from(context));
        setContentView(bind.getRoot());
        initView();
    }


    public BottomSinglePickerDialog setMessage(ArrayList<String> message) {
        this.message = message;
        return this;
    }
    public BottomSinglePickerDialog setTitle(String title) {
        this.title = title;
        return this;
    }

    public BottomSinglePickerDialog setOnClickListener(ICell.ICell2<Integer,String> onClickListener) {
        this.onClickListener = onClickListener;
        return this;
    }

    public BottomSinglePickerDialog setOutIsHind(boolean outIsHind) {
        this.outIsHind = outIsHind;
        this.setCanceledOnTouchOutside(outIsHind);
        return this;
    }

    String selectMsg = "";
    int position = -1;
    private void initView() {
        setCancelable(true);
        this.setCanceledOnTouchOutside(outIsHind);
        //设置其大小
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = CommonUtils.getScreenHeight();
        window.setAttributes(lp);
        bind.tvDialogTitle.setText(title);
        ArrayWheelAdapter arrayWheelAdapter = new ArrayWheelAdapter(message);
        bind.wheelView.setItemsVisibleCount(3);
        bind.wheelView.setAdapter(arrayWheelAdapter);
        bind.wheelView.setTextSize(15f);
        bind.wheelView.setCyclic(false);
        bind.wheelView.setCurrentItem(CurrentItem);
        bind.wheelView.setLineSpacingMultiplier(14);

        bind.wheelView.setOnItemSelectedListener(index -> {
            LogUtils.i("onItemSelected: " + message.get(index));
            selectMsg = message.get(index);
            position = index;
        });
        selectMsg = message.get(CurrentItem);
        position = CurrentItem;
        bind.conlyDialogsingonParent.setOnClickListener(v ->{
            if(outIsHind) {
                dismiss();
            }
        });
        bind.qmuirelativeDialogsigonParent.setOnClickListener(v -> {});
        bind.tvDialogSingonpickerSubmit.setOnClickListener(v -> {
            if (onClickListener != null)
                onClickListener.cell(position,selectMsg);
            dismiss();
        });
        bind.tvDialogCancle.setOnClickListener(v -> {
            dismiss();
        });
    }

    @Override
    public void show() {
        if (message.isEmpty()){
            Toaster.show("没有数据");
            return;
        }
        super.show();
    }
}
